--lua script
--活动：押镖活动通用处理
--#include "data\functions\Quest\QuestCommon.lua" once 				--任务相关方法
--#include "data\functions\Systems\ActivitySystem\ActivityUtils.lua" once


-- @brief: 押镖超时
function OnActorDartExpired(sysarg)	
	-- 失败处理
	OnActorDartFailed(sysarg, StdActivityCfg[tagActivityType.DBYBActivity].DartSetup.CampDartFailExpired)
end

--开始护送
--[[
    函数名称：StartGuildDart
    函数描述：创建镖车
    参数 sysarg 的描述：
    参数 type 的描述：是否随机选择 1随机 0不随 不随机默认选1 
 ]]

function StartApplyActorDart(sysarg, type)
	local dvar = Actor.getDyanmicVar(sysarg)
	local sVar = Actor.getStaticVar(sysarg)
	
	if sVar.bcpz == nil or sVar.bcpz == 0 then
		sVar.bcpz = 1
	end

	local bcpz = sVar.bcpz
	if type == 1 then 	
		-- 随机选一个镖车
		local randCnt = System.getRandomNumber(100)  -- 返回[0-100]的随机数	
		for i=1, table.getn(EscortDartConfig.DartHorse[bcpz].freshRate[1]) do
			if (randCnt < EscortDartConfig.DartHorse[bcpz].freshRate[1][i]) then
				bcpz = i
				break
			end		
		end
	end

	local monsterInfo = EscortDartConfig.DartHorse[bcpz]    --给予一档的镖车
	sVar.bcpz = bcpz

	if monsterInfo ~= nil then
		local monster = sVar.bcHandle or 0
		if monster > 0 then
			Fuben.clearMonster(monster, false)
		end

		local bool = Actor.refreshActorObserveList(sysarg)     --刷新观察者，防止客户端延迟
	end
end

-- 请求开始押镖
function StartDart(sysarg, packet)
	local buybx = DataPack.readByte( packet );	--是否购买保险
	local cfg = StdActivityCfg[tagActivityType.DBYBActivity];
	local var = Actor.getStaticVar(sysarg)
	local ybstate = var.ybstate;
	if not ybstate then ybstate = 0; end
	if ybstate == 1 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.bc00010, ttFlyTip)
		return 
	end
	
	if not buybx then buybx = 0; end
	if buybx == 1 then
		if Actor.changeMoney(sysarg, mtYuanbao, -cfg.insurance, LogIds.ybBuyInsuranceConsume, Lang.LogTips.yb049) == false then
			--Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.bc00009, cfg.insurance), ttFlyTip);
			SendRecommendTips(sysarg, PopUpWndType.PayWindow, Lang.ScriptTips.COM0009);
			var.buybx = nil;
			return;
		end
		var.buybx = buybx;
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.bc00001, cfg.insurance, cfg.tAwardPercent[4]*100), ttFlyTip);
	end
	
	StartDartToDest(sysarg)
	Actor.closeDialogs(sysarg, enLinkYuanbaoWnd);
end

-- 开始押镖
function StartDartToDest(sysarg)
	local cfg = StdActivityCfg[tagActivityType.DBYBActivity];
	local var = Actor.getStaticVar(sysarg)
	local dvar = Actor.getDyanmicVar(sysarg)
	if not var.days then var.days = {} end
	local darttms = var.days.darttms;
	if not darttms then darttms = 0; end

	if darttms >= cfg.maxEscort then
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.bc00013, darttms), ttFlyTip);
		return;
	end
	
	if not var.bcpz then var.bcpz = 1 end
	local bcpz = var.bcpz
	local monsterInfo = cfg.tBiaoche[bcpz]    --给予一档的镖车

	if monsterInfo ~= nil then
		local monster = var.bcHandle or 0
		if monster > 0 then
		  --通知客户端清除怪物信息 
			local pack = DataPack.allocPacket(sysarg, LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sClearMonters)
			DataPack.writeUint64(pack, monster)
			DataPack.flush(pack)
			Fuben.clearMonster(monster, false)
		end
		
		-- 创建镖车
		local x, y = Actor.getEntityPosition(sysarg, 0, 0)		--获取实体X Y坐标
		if CreateDartCar(sysarg, cfg, monsterInfo, x, y, cfg.time) == true then
			var.ybstate = cfg.DartSetup.CampDartStatusAccept
			Actor.onTakeOffRide(sysarg);
			var.ActorDartTime = System.getCurrMiniTime() --开始护镖时间 
			
			var.days.darttms = darttms + 1
			UpdateDartInfo(sysarg, var, cfg, monsterInfo, var.ActorDartDelayTime);
			
		--	Actor.addBuffById(sysarg, cfg.buff[1].id)
			
			local RwData = cfg.RwData;
			local name = Actor.getName(sysarg)
			local bcname = string.format("{color;%s;%s}", monsterInfo.color, monsterInfo.monName);
			System.broadcastTipmsg(string.format(Lang.ScriptTips.bc00005, name, bcname, RwData[3]), ttChatWindow)
			return true;
		end
	end
	return false;
end

-- 更新押镖信息
function UpdateDartInfo(sysarg, var, cfg, monsterInfo, startTime)
	local pack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sUpdateActivityData)
	if nil == pack then
		return;
	end

	DataPack.writeInt(pack, cfg.id)						-- 活动id
	local name = string.format("{color;%s;%s}", monsterInfo.color, monsterInfo.monName);
	DataPack.writeString(pack, name)				-- 镖车名

	-- 镖车保险
	local buybx = var.buybx		-- 保险
	if nil == buybx then buybx = 0; end

	DataPack.writeByte(pack, buybx);
	DataPack.writeInt(pack, startTime)  --镖车剩余时间
	
	local nExp = 0;				-- 经验
	local nBindCion = 0;	-- 金币
	local nMana = 0;			-- 灵力
	local times = 0; -- 活动剩余时间
	local nMultiple = 1;
	local isDouble = 0;

	local sdvar = System.getDyanmicVar()
	if nil ~= sdvar.dpybstart then
		nMultiple = 2;
		isDouble = 1;
	end

	-- 是否双倍活动时间
	DataPack.writeByte(pack, isDouble);			-- 是否有双倍, 1有, 0没有
	nExp = monsterInfo.Awards[1].count * nMultiple;
	nBindCion = monsterInfo.Awards[2].count * nMultiple;
	nMana = monsterInfo.Awards[3].count * nMultiple;

	if buybx == 1 then
		nExp = nExp * cfg.tAwardPercent[4];
		nBindCion = nBindCion * cfg.tAwardPercent[4];
		nMana = nMana * cfg.tAwardPercent[4];
	end

	if nil ~= sdvar.dpybtime then
		times = sdvar.dpybtime + cfg.activitytime - System.getCurrMiniTime();
		if times < 0 then times = 0; end
	else
		times = 0;
	end
	
	DataPack.writeInt(pack, nExp);		-- 经验
	DataPack.writeInt(pack, nBindCion);	-- 金币
	DataPack.writeInt(pack, nMana);		-- 战将灵力
	DataPack.writeInt(pack, times);		-- 活动剩余时间
	DataPack.flush(pack)
end


function dartFaildSendMail(sysarg)
	local cfg = StdActivityCfg[tagActivityType.DBYBActivity];
	local tBiaoche = cfg.tBiaoche;
	local var = Actor.getStaticVar(sysarg)
	local bcpz = var.bcpz;
	if not bcpz or bcpz > #tBiaoche then
		print("dard faild, send failed mail error");
		return;
	end

	local ybfail = var.ybfail;
	if not ybfail then ybfail = 0; end
	if 0 == ybfail then
		return;		-- 没有失败就不发邮件
	end

	local buybx = var.buybx;
	if nil == buybx then buybx = 0; end

	local tAwardPercent = cfg.tAwardPercent;
	local tAwards = tBiaoche[bcpz].Awards;

	local sdvar = System.getDyanmicVar()
	local nMultiple = 1;
	if nil ~= sdvar.dpybstart then nMultiple = 2; end

	-- 计算奖励
	local exp = tAwards[1].count;
	local bindcion = tAwards[2].count;
	local mana = tAwards[3].count;
	local profit = 0;
	local failReturn = cfg.failReturn;		-- 保证金
	if buybx == 1 then
		-- 买了保险
		tAwards[1].count = exp * nMultiple * tAwardPercent[3];
		tAwards[2].count = bindcion * nMultiple * tAwardPercent[3];
		tAwards[3].count = mana * nMultiple * tAwardPercent[3];
		profit = 100;
		failReturn = cfg.failReturn;		-- 保证金
	else
		tAwards[1].count = exp * nMultiple * tAwardPercent[1];
		tAwards[2].count = bindcion * nMultiple * tAwardPercent[1];
		tAwards[3].count = mana * nMultiple * tAwardPercent[1];
		profit = 50;
		failReturn = 0;
	end

	
	local actorID = Actor.getIntProperty(sysarg, PROP_ENTITY_ID);
	local strTitle = Lang.MailTitle[11];


	--描述: "{很遗憾您本次押镖失败了.}获得{%d％收益}: {经验x%d}、{金币x%d}、{灵力x%d}、{保证金x%d}",
	local strDesc = string.format(Lang.MailDesc[14], profit, tAwards[1].count, tAwards[2].count, tAwards[3].count, failReturn);
	local strData = "";

	-- 格式: type,id,count;
	for _, v in ipairs(tAwards or {}) do
		if nil ~= v then
			local str = "";
			str = string.format("%d,%d,%d;", v.type, v.id, v.count);
			strData = string.format("%s%s", strData, str);
		end
	end

	-- 保险金
	strData = string.format("%s15,0,%d;", strData, failReturn);

	Awards.GiveToMailById(actorID, {}, strTitle, strDesc)
end

-- 交镖
function SubmitDart(sysarg)
	local var = Actor.getStaticVar(sysarg)
	if not var.ybstate or var.ybstate ~= 1 then
		return false, 0, 0, 0, 0, 0, 0, 0;
	end
	
	if ChechDartDist(sysarg) == 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.bc00015, ttFlyTip);
		return false, 0, 0, 0, 0, 0, 0, 0;
	end
	
	local cfg = StdActivityCfg[tagActivityType.DBYBActivity];
	local tBiaoche = cfg.tBiaoche;
	local bcpz = var.bcpz;
	if not bcpz or bcpz > #tBiaoche then
		ResetActorDartInfo(sysarg, false, true);
		return false, 0, 0, 0, 0, 0, 0, 0;
	end

	-- 活动期间有双倍
	local nlogid = 0;
	local slogtip = "";
	local nMultiple = 1;
	local sdvar = System.getDyanmicVar()
	if nil == sdvar.dpybstart then
		nlogid = LogIds.NormalDartAward;
		slogtip = Lang.LogTips.dt001;
	else
		nlogid = LogIds.MultipleDartAward;
		slogtip = Lang.LogTips.dt002;
		nMultiple = 2;
	end
	
	local buybx = var.buybx;
	if not buybx then buybx = 0; end
	
	local tAwardPercent = cfg.tAwardPercent;
	local tAwards = tBiaoche[bcpz].Awards;

	local ybfail = var.ybfail;
	if not ybfail then ybfail = 0; end
	
	local exp = tAwards[1].count;
	local bindcion = tAwards[2].count;
	local mana = tAwards[3].count;
	
	if buybx == 1 then
		-- 买了保险
		if ybfail == 0 then
			-- 成功了
			tAwards[1].count = exp * nMultiple * tAwardPercent[4];
			tAwards[2].count = bindcion * nMultiple * tAwardPercent[4];
			tAwards[3].count = mana * nMultiple * tAwardPercent[4];
			
			-- 返还保险费
			Actor.changeMoney(sysarg, mtYuanbao, cfg.insurance, LogIds.DartInsuranceReturn, Lang.LogTips.dt003);
		else
			tAwards[1].count = exp * nMultiple * tAwardPercent[3];
			tAwards[2].count = bindcion * nMultiple * tAwardPercent[3];
			tAwards[3].count = mana * nMultiple * tAwardPercent[3];
		end
	else
		if ybfail == 0 then
			-- 成功了
			tAwards[1].count = exp * nMultiple * tAwardPercent[2];
			tAwards[2].count = bindcion * nMultiple * tAwardPercent[2];
			tAwards[3].count = mana * nMultiple * tAwardPercent[2];
		else
			tAwards[1].count = exp * nMultiple * tAwardPercent[1];
			tAwards[2].count = bindcion * nMultiple * tAwardPercent[1];
			tAwards[3].count = mana * nMultiple * tAwardPercent[1];
		end
	end
	
	-- 发放奖励
	if tAwards then
		Actor.addExp(sysarg, tAwards[1].count, nlogid, 0);		-- 经验
		Actor.changeMoney(sysarg, mtBindCoin, tAwards[2].count, nlogid, slogtip);	-- 金币
		local mana = Hero.getHeroMana(sysarg);
		mana = mana + tAwards[3].count;
		Hero.setHeroMana(sysarg, mana);
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.bc00016, tAwards[3].count), ttTipmsgWindow);
	end
	
	--押镖完成的总次数
	if var.dtTotaltms == nil then
		var.dtTotaltms = 1
	else
		var.dtTotaltms = var.dtTotaltms + 1
	end

	-- 护送镖车总次数,参数1表示总次数
	Actor.triggerAchieveEvent(sysarg, 101, var.dtTotaltms)

	send_finished_quest(sysarg, DailyTaskType.TYPE_FSYB)
	ResetActorDartInfo(sysarg, false, true)
	
	--FinishLivenessTask(sysarg, LivenessActivityCfg.livenessTask.hsbc, 1)
	
	-- {镖车品质, 买了保险, 奖励1, 奖励1, 奖励1}
	return true, bcpz, buybx, ybfail, cfg.insurance, tAwards[1].count, tAwards[2].count, tAwards[3].count;
end

--发送下属
function SendActorDartInfo(sysarg,monsterId)
	local dVar = Actor.getStaticVar(sysarg)
	local svar = Actor.getDyanmicVar(sysarg)
	
	local cfg = StdActivityCfg[tagActivityType.DBYBActivity];
	local dest = cfg.Dest  --目标 信息

	if dVar.ActorDartName == nil or svar.bcHandle == nil then
		return
	end

	if dest ~= nil then
	    		--Actor.regScriptCallback(monster, nil, 15000, 15000, -1, "OnCheckFollowDist")
				-- 发消息通知客户端设置归属。消息内容：归属怪物句柄、是否有目标点、[目标场景id、目标npc名称]				
		local pack = DataPack.allocPacket(sysarg, LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sSetAscription)
		if (pack ~= nil) then
			DataPack.writeUint64(pack,svar.bcHandle)    --下属句柄
			DataPack.writeString(pack,dVar.ActorDartName)		-- 下属名称							
			DataPack.writeInt(pack, monsterId)			-- 下属模型Id
			DataPack.writeByte(pack, 1)                            --消息中是否有目标点信息 1表示后面紧跟着目标点信息 0表示后面无目标点信息
			DataPack.writeInt(pack, dest.destsceneid)       --目标点场景ID
			DataPack.writeString(pack, dest.destnpcname)    --目标NPC名称
			DataPack.flush(pack)
		end
	end
end

--下发移除下属
function DeleteActorDartInfo(sysarg, bcHandle)
	local pack = DataPack.allocPacket(sysarg, LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sClearMonters)
	if (pack ~= nil) then
		DataPack.writeUint64(pack, bcHandle)    --下属句柄
		DataPack.flush(pack)
	end
end

-- 打开押镖对话框
function OpenDartDialog(sysarg, packet)
	local cfg = StdActivityCfg[tagActivityType.DBYBActivity];
	local pLv = Actor.getIntProperty(sysarg,PROP_CREATURE_LEVEL);
	if pLv < cfg.level then
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.bc00006, cfg.level), ttFlyTip);
		return;
	end
	
	local var = Actor.getStaticVar(sysarg);
	if not var.days then var.days = {} end
	local bcpz = var.bcpz;
	if not bcpz then bcpz = 1 end
	
	local pzxstms = var.pzxstms;
	if not pzxstms then pzxstms = 0; end
	
	local darttms = var.days.darttms;
	if not darttms then darttms = 0; end

	if darttms >= cfg.maxEscort then
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.bc00013, darttms), ttFlyTip);
		return;
	end
	
	local tBiaoche = cfg.tBiaoche[bcpz];
	if not tBiaoche then
		return;
	end
	
	local bDouble = 0;
	local sdvar = System.getDyanmicVar()
	if nil ~= sdvar.dpybstart then
		bDouble = 1;
	end

	local sData = tostring(bcpz)..","..tostring(pzxstms)..","..tostring(darttms)..","..tostring(bDouble)
	return sData;
end

-- 刷新镖车品质
function ActorChangeSlave(sysarg, packet)
	local bcheck = DataPack.readByte( packet );	--是否勾选元宝购买运镖令
	local cfg = StdActivityCfg[tagActivityType.DBYBActivity];
	local var = Actor.getStaticVar(sysarg);
	if var.ybstate == 1 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.bc00010, ttFlyTip)
		return 
	end
	
	local bcpz = var.bcpz;
	if not bcpz then bcpz = 1; end
	
	local pzxstms = var.pzxstms;
	if not pzxstms then pzxstms = 0; end
	
	tBiaoche = cfg.tBiaoche;
	if (bcpz + 1) > #tBiaoche then
		local name = string.format("{color;%s;%s}", tBiaoche[bcpz].color, tBiaoche[bcpz].monName);
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.bc00008, name), ttFlyTip);
		return
	end
	
	if pzxstms >= cfg.maxRefresh then
		local tYBL = cfg.tYBL;
		if Actor.removeItem(sysarg, tYBL.id, tYBL.count, -1, -1, -1, "扣除运镖令") ~= tYBL.count then
			if bcheck == 1 then
				if Actor.changeMoney(sysarg, mtYuanbao, -tYBL.yb, LogIds.ybBuyYBLConsume, Lang.LogTips.yb050) == false then
					Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.bc00012, tYBL.name, tYBL.yb), ttFlyTip);
					return
				end
			else
				Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.bc00004, tYBL.name, tYBL.count), ttFlyTip);
				return
			end
		end
	end

	var.pzxstms = pzxstms + 1;
	if var.pzxstms > cfg.maxRefresh then
		var.pzxstms = cfg.maxRefresh;
	end
	
	if math.random(10000) > tBiaoche[bcpz + 1].freshRate then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.bc00003, ttFlyTip);
		local pack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sDartRefreshPuality)
		if (pack ~= nil) then
			DataPack.writeByte(pack, bcpz)    		-- 镖车品质
			DataPack.writeByte(pack, var.pzxstms)			-- 刷新次数
			DataPack.flush(pack)
		end
		return
	end
	
	var.bcpz = bcpz + 1;
	local name = string.format("{color;%s;%s}", tBiaoche[var.bcpz].color, tBiaoche[var.bcpz].monName);
	Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.bc00002, name), ttFlyTip);
	
	local pack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sDartRefreshPuality)
	if (pack ~= nil) then
		DataPack.writeByte(pack, var.bcpz)    		-- 镖车品质
		DataPack.writeByte(pack, var.pzxstms)			-- 刷新次数
		DataPack.flush(pack)
	end
end


-- @brief: 护送失败处理处理。(type = 1护镖超时、3镖车死亡、4玩家放弃、5玩家退出游戏、6玩家死亡 镖车被劫)
function OnActorDartFailed(sysarg, reason)
	local cfg = StdActivityCfg[tagActivityType.DBYBActivity];
	local DartSetup = cfg.DartSetup;
	reason = reason or cfg.DartSetup.CampDartFailUnknown
	local var = Actor.getStaticVar(sysarg)
	var.ybfail = 1;

	dartFaildSendMail(sysarg);
	ResetActorDartInfo(sysarg, false, true)
	exit_activity(sysarg, tagActivityType.DBYBActivity);
	send_giveup_quest(sysarg, DailyTaskType.TYPE_FSYB);

	if reason == DartSetup.CampDartFailObjDie then	-- 被杀
		Actor.sendTipmsg(player, Lang.ScriptTips.bc00011, ttFlyTip);
	end
end

-- @brief: 重置护送相关的数据
function ResetActorDartInfo(sysarg, bDelayRemove, bResetData)
	if (bDelayRemove == nil) then bDelayRemove = false end

	local dVar = Actor.getDyanmicVar(sysarg)
	local var = Actor.getStaticVar(sysarg)

	-- 清除镖车
	if var.bcHandle ~= nil and var.bcHandle ~= 0 then
		Fuben.clearMonster(var.bcHandle, bDelayRemove)
	end

	Actor.unregScriptCallback(sysarg, "OnActorDartExpired")

	dVar.ActorDartacceptNpc = nil
	dVar.ActorDartdestnpcname = nil;
	dVar.ActorDartdestsceneid = nil;
	
	var.pzxstms = nil;
	var.buybx = nil;
	var.ybfail = nil;
	var.bcHandle = nil;
	var.ActorDartDelayTime = nil
	var.ybstate = nil
	var.bcpz = nil
	var.ActorDartTime = nil
	var.bcHandle = nil
	var.ybfail = nil;
	
	Actor.setDartFlag(sysarg, false)
end


-- 复活镖车
function ReliveDart(sysarg)
	local var = Actor.getStaticVar(sysarg)
	if var.ybstate == 1 and var.bcpz then
		local bccfg = StdActivityCfg[tagActivityType.DBYBActivity].tBiaoche[var.bcpz];
		local x, y;
		if not var.bcposx or not var.bcposy then
				x, y = Actor.getEntityPosition(sysarg, 0, 0)		--获取实体X Y坐标
		else
			x = var.bcposx;
			y = var.bcposy;
		end
		
		local nTime = System.getCurrMiniTime()
		local lastTime = var.ActorDartTime + StdActivityCfg[tagActivityType.DBYBActivity].time - nTime
		if CreateDartCar(sysarg, cfg, bccfg, x, y, lastTime) == true then
			UpdateDartInfo(sysarg, sVar, cfg, bccfg, var.ActorDartDelayTime);
		end
	end
	Actor.unregScriptCallback(sysarg, "ReliveDart");
end



-- 创建镖车
function CreateDartCar(sysarg, cfg, bccfg, x, y, time)
	local dvar = Actor.getDyanmicVar(sysarg)
	local sVar = Actor.getStaticVar(sysarg)
	local monster = Actor.createMonster(sysarg, bccfg.id, x, y, 0)
	if monster ~= nil then
		Actor.followActor(monster, sysarg, cfg.distance[1], cfg.distance[2], true) --跟随某个玩家
		Actor.setEntityTelByOwnderAttr(monster, true)   --设置实体是否跟着归属者传送
		Actor.setEntityLazyFollow(monster, true)        --设置实体是否惰性跟随
		Actor.setDenyAttackedByActor(monster, false)     --设置实体是否可被玩家攻击
		Actor.refreshActorObserveList(sysarg)     --刷新观察者，防止客户端延迟 
		
		-- 保存开始护镖时间、怪物指针和接镖NPC、目标NPC名称和目标NPC所在场景Id 奖励倍率
		sVar.bcHandle = Actor.getHandle(monster) --怪物句柄
		
		dvar.ActorDartacceptNpc = Actor.getHandle(thisNPC)  -- 貌似直接保存指针保存不了，这里保存句柄
		dvar.ActorDartdestnpcname = cfg.Dest.name  --目标NPC名称
		dvar.ActorDartdestsceneid = cfg.Dest.sceneid  --目标点场景ID
		dvar.ActorDartName = Actor.getName(monster)	--怪物名称

		SendActorDartInfo(sysarg, Actor.getIntProperty(monster, PROP_ENTITY_MODELID))
		
		-- 处理护镖超时
		Actor.unregScriptCallback(sysarg, "OnActorDartExpired")
		
		sVar.ActorDartDelayTime = time
		if sVar.ActorDartDelayTime < 0  then
			sVar.ActorDartDelayTime = 0;
		end
		if sVar.ActorDartDelayTime >= 0 then
			Actor.regScriptTimer(sysarg, 0, sVar.ActorDartDelayTime*1000, 10000, 1, "OnActorDartExpired")
		end
		return true;
	end
	return false;
end

-- 放弃护送
function AbandonEscort(sysarg, packet)
	ResetActorDartInfo(sysarg, false, true);
	send_giveup_quest(sysarg, DailyTaskType.TYPE_FSYB);		-- 下发放弃

	local pack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sAbandonEscort)
	DataPack.writeByte(pack, 1)
	DataPack.flush(pack)
end

-- 继续护送
function ContinueEscort(sysarg, packet)
	local var = Actor.getStaticVar(sysarg)
	if var.ybstate ~= 1 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.bc00014, ttFlyTip);
		return;
	end
	
	local bcHandle = var.bcHandle;
	if not bcHandle then
		return;
	end
	
	local bcptr = Actor.getEntity(bcHandle);
	if not bcptr then
		print("bcptr is nil, bcHandle "..bcHandle);
		return;
	end

	local sceneid = Actor.getSceneId(bcptr);
	local sceneName, x, y = Actor.getSceneName( bcptr, 0, 0 );
	
	-- 下发移动到镖车旁
	local pack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sMoveToDartPos)
	DataPack.writeInt(pack, sceneid)
	DataPack.writeString(pack, sceneName);
	DataPack.writeWord(pack, x)
	DataPack.writeWord(pack, y - 1);
	DataPack.flush(pack)
end

-- 使用无敌镖车技能
function UseInvincibleSkill(sysarg, packet)
	local var = Actor.getStaticVar(sysarg)
	if var.ybstate ~= 1 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.bc00014, ttFlyTip);
		return;
	end
	
	local bcHandle = var.bcHandle;
	if not bcHandle then
		return;
	end
	
	local buff = StdActivityCfg[tagActivityType.DBYBActivity].buff[2];
	local bcptr = Actor.getEntity(bcHandle);
	if not bcptr then
		print("bcptr is nil");
		return
	end
	Actor.addBuffById(bcptr, buff.id, buff.value)
end

-- 

-- 检测镖车距离
function ChechDartDist(sysarg)
	local dVar = Actor.getDyanmicVar(sysarg)
	local svar = Actor.getStaticVar(sysarg)

	local monster = Actor.getEntity(svar.bcHandle)
	if not monster then
		print("dart error bcHandle error");
	end

	local playerX,playerY = Actor.getEntityPosition(sysarg, 0, 0)
	local monsterX,monsterY = Actor.getEntityPosition(monster, 0, 0)
	
	monsterX = playerX - monsterX
	monsterY = playerY - monsterY
	
	local Distance = (monsterX*monsterX) + (monsterY*monsterY)
	if Distance > (10*10) then
		return 0;
	end
	return 1;
end


-- 行会求救技能
function UseGuildHelpSkill(sysarg, packet)
	
end

-- 押镖活动开始
function dpyb_activity_start()
	local sdvar = System.getDyanmicVar()
	sdvar.dpybstart = 1;
	sdvar.dpybtime = System.getCurrMiniTime();
end


-- 押镖活动结束
function dpyb_activity_end()
	local sdvar = System.getDyanmicVar()
	sdvar.dpybstart = nil;
	sdvar.dpybtime = nil;
end
